using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._1106;

public class UserService : IUserService
{
    private readonly List<User> _users = new List<User>();

    public bool Register(string username, string password, string confirmPassword)
    {

        if (string.IsNullOrEmpty(username))
            return false;

        if (username.Length < 3 || username.Length > 15)
            return false;

        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            return false;


        foreach (var user in _users)
        {
            if (user.Username == username)
                return false;
        }


        if (string.IsNullOrEmpty(password) || password.Length < 6 || password.Length > 20)
            return false;

        bool hasUpper = password.Any(char.IsUpper);
        bool hasLower = password.Any(char.IsLower);
        bool hasDigit = password.Any(char.IsDigit);
        if (!hasUpper || !hasLower || !hasDigit)
            return false;

        if (password != confirmPassword)
            return false;


        _users.Add(new User(username, password));
        return true;
    }

    public bool Login(string username, string password)
    {
        foreach (var user in _users)
        {
            if (user.Username == username && user.Password == password)
                return true;
        }
        return false;
    }

    public void ShowMainMenu()
    {
        while (true)
        {
            Console.Clear();
            Console.WriteLine("===== 主菜单 =====");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.WriteLine("==================");
            Console.Write("请选择功能(1-3): ");

            string input = Console.ReadLine()?.Trim();
            if (!int.TryParse(input, out int choice))
            {
                Console.WriteLine("请输入有效数字！");
                WaitForKeyPress();
                continue;
            }

            switch (choice)
            {
                case 1:
                    HandleLogin();
                    break;
                case 2:
                    HandleRegister();
                    break;
                case 3:
                    Console.WriteLine("感谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("请输入1-3之间的数字！");
                    WaitForKeyPress();
                    break;
            }
        }
    }

    private void HandleLogin()
    {
        Console.Clear();
        Console.WriteLine("===== 用户登录 =====");
        
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        Console.Write("请输入密码: ");
        string password = ReadPassword();

        if (Login(username, password))
        {
            Console.WriteLine("\n登录成功！欢迎回来，{0}！", username);
        }
        else
        {
            Console.WriteLine("用户名或密码错误！");
        }
        WaitForKeyPress();
    }

    private void HandleRegister()
    {
        Console.Clear();
        Console.WriteLine("===== 用户注册 =====");
        
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        Console.Write("请输入密码: ");
        string password = ReadPassword();
        
        Console.Write("请确认密码: ");
        string confirmPassword = ReadPassword();

        if (Register(username, password, confirmPassword))
        {
            Console.WriteLine("注册成功！");
        }
        else
        {
            Console.WriteLine("注册失败，请检查输入信息！");
        }
        WaitForKeyPress();
    }

    private string ReadPassword()
    {
        string password = "";
        ConsoleKeyInfo key;

        do
        {
            key = Console.ReadKey(true);
            if (key.Key == ConsoleKey.Backspace && password.Length > 0)
            {
                password = password.Substring(0, password.Length - 1);
                Console.Write("\b \b");
            }
            else if (key.Key != ConsoleKey.Enter)
            {
                password += key.KeyChar;
                Console.Write("*");
            }
        } while (key.Key != ConsoleKey.Enter);

        Console.WriteLine();
        return password;
    }

    private void WaitForKeyPress()
    {
        Console.WriteLine("按任意键继续...");
        Console.ReadKey();
    }
}